package com.easylinkin.linkappapi.meterbilling.controller;

import com.alibaba.fastjson.JSON;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.meterbilling.entity.MeterDeviceConfig;
import com.easylinkin.linkappapi.meterbilling.service.MeterDeviceConfigService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @program: linkapp-group
 * @description: 水电表管理
 * @author: chenkaixuan
 * @create: 2021-07-12 15:09
 */
@RestController
@RequestMapping("/cbjf/deviceConfig")
public class MeterDeviceConfigController {
    @Autowired
    MeterDeviceConfigService service;

    @PostMapping("page")
    public RestMessage page(@RequestBody RequestModel<MeterDeviceConfig> requestModel){
        Assert.notNull(requestModel, "参数为空");
        Assert.notNull(requestModel.getPage(), "参数page为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams参数为空");
        return  RestBuilders.successBuilder().data(service.getMeterDeviceConfigList(requestModel.getPage(),requestModel.getCustomQueryParams())).build();
    }

    @PostMapping("getAll")
    public RestMessage getAll(@RequestBody MeterDeviceConfig requestModel){
        return  RestBuilders.successBuilder().data(service.getMeterDeviceConfigList(requestModel)).build();
    }

    @PostMapping("getDeviceAll")
    public RestMessage getDeviceAll(@RequestBody MeterDeviceConfig meterDeviceConfig){
        return  RestBuilders.successBuilder().data(service.getDeviceAll(meterDeviceConfig)).build();
    }

    @PostMapping("get")
    public RestMessage get(@RequestBody MeterDeviceConfig requestModel){
        return  RestBuilders.successBuilder().data(service.getMeterDeviceConfig(requestModel)).build();
    }

    @PostMapping("saveOrUpdate")
    public RestMessage save(@RequestBody MeterDeviceConfig meterDeviceConfig){
      service.saveEntity(meterDeviceConfig);
      return  RestBuilders.successBuilder().build();
    }

    @PostMapping("config")
    public RestMessage batchConfig(@RequestBody MeterDeviceConfig meterDeviceConfig){
        service.batchConfig(meterDeviceConfig);
        return  RestBuilders.successBuilder().build();
    }

    @PostMapping("removeIds")
    public RestMessage remove(@RequestBody MeterDeviceConfig meterDeviceConfig){
        service.removeIds(meterDeviceConfig);
        return  RestBuilders.successBuilder().build();
    }

    /***
     * 下载导入模板
     * @param request
     * @param response
     */
    @GetMapping("getImportTemplate")
    public void getImportTemplate(HttpServletRequest request, HttpServletResponse response) {
        service.getImportTemplate(request, response);
    }

    @PostMapping("importData")
    public RestMessage importData(@RequestParam(value = "file") MultipartFile file) {
        Assert.notNull(file, "文件不能为空");
        Map<Integer, String> resultMap= service.importData(file);
        if(resultMap.isEmpty()) {
            return RestBuilders.successBuilder().data("导入成功").build();
        }else {
            Collection<String> values = resultMap.values();
            StringBuilder errorMsgTemp = new StringBuilder();
            values.forEach(error -> {
                errorMsgTemp.append(";").append(error);
            });
            String errorMsg = errorMsgTemp.toString().substring(1);
            return RestBuilders.successBuilder().data(errorMsg).build();
        }
    }

    @PostMapping("exportData")
    @ApiOperation("导出")
    public void exportData(@RequestBody MeterDeviceConfig meterDeviceConfig, HttpServletRequest request, HttpServletResponse response) {
        service.exportData(meterDeviceConfig, request, response);
    }
}
